$(function() {
	//var url_findEventDetail = "/ag/dgm/findEventDetailEventReport.action?event.EId=1008011002";
	var url_auditReport="/ag/dgm/auditReport_eventReport.action";			//审核报告

	var url_loadRecord="/ag/dgm/loadEventReportString_eventReport.action";		
	var eventId=$.trim($('.eventId').text());

	//重置查询条件
	$("#q_rest").click(function(){
		document.getElementById("eventLevel").selectedIndex=0;
		document.getElementById("eventStatus").selectedIndex=0;
		document.getElementById("eventType").selectedIndex=0;
		document.getElementById("eventSource").selectedIndex=0;
		document.getElementById("order").selectedIndex=0;
		$("#eId").val("");
		$("#eventTimeStart").val("");
		$("#eventTimeEnd").val("")
	});
	
	
	$.ajax({
  		type: "post",
 		url: "dgm/checkStatusByEventId_eventReport.action",
 		data:{'tbEventIdString':eventId},
  		success:function(data){
			if(data.result=="N"){
			alert("事件状态不是处置结束，不可办结");
			window.location.href="/ag/wzp/event_findById.action?event.EId="+eventId;		//回到事件详情中
			}
		}}
	);	
	
	
	$.post(url_loadRecord,{'tbEventIdString':eventId},function(data){		//刚进页面时把办结报告load下来										//刚开始的时候取报告的内容
		var eventReportDOM1 = $("#xhe0_iframe").contents();
		var eventReport1=$(eventReportDOM1).find(".editMode");
		$(eventReport1).html(data.eventReportString);
		if(data.eventReportString != null && data.eventReportString === "") {
			$("#tips").append("请审核报告！");
		}
	});
	/*
	 * 报告提交
	 */
	$(".submitIT").click(function() {
		$("#eventStatus").append("确定保存并提交?");
		$("#eventStatus").dialog('open');
	});
	
	$("#eventStatus").dialog( {
		autoOpen : false,
		height : 170,
		width : 350,
		modal : true,
		buttons : {
			'确定' : function() {
			submitER();
			$("#eventStatus").empty();
			$("#eventStatus").dialog('close');
//			location.href="/ag/dgm/findEventDetailEventReport.action?event.EId=1008011002";
			},
			'取消':function(){
				$("#eventStatus").empty();
				$("#eventStatus").dialog('close');
			}
		},
		close : function() {
			$("#eventStatus").empty();
			}
	});
	/*
	 * 审核提交
	 */
	$(".auditIT").click(function() {
		$("#eventReportAudit").dialog('open');
	});
	
	$("#eventReportAudit").dialog( {
		autoOpen : false,
		height : 170,
		width : 450,
		modal : true,
		buttons : {
			'确定' : function() {
		var auditContent = $("#auditContent").text();
			if(checkLength(auditContent,"内容",0,500)){
	     		var ifagree = $("#ifagree").attr("checked");
	     		if(ifagree==true){
	     			var agreeif="Y";
	     		}
	     		else{
	     			var agreeif="N";
	     		}
	     		auditSubmit(auditContent,agreeif,eventId);
	     		clearEventReportAudit();
			}else{
				result_alert("提交失败，请将字数控制在0到500内");
			}
			$("#eventReportAudit").dialog("close");
			},
			'取消': function(){
				clearEventReportAudit();
				$("#eventReportAudit").dialog("close");
			}
		},
		close : function() {
			clearEventReportAudit();
			}
	});
	
	function auditSubmit(auditContent,agreeif,eventId){
		var req_auditReport={
				'auditAgree':agreeif,
				'auditContent':auditContent,
				'tbEventIdString':eventId
		};
		$.post(url_auditReport,req_auditReport,function(data){
				var agreeString ="办结报告不通过";
			if(agreeif=="Y"){
				agreeString = "办结报告通过";
			}
			result_alert("审核人:"+data.tbUser.UName+","+agreeString);
		});
	}
	
	function clearEventReportAudit() {
		$("#auditContent").text("");
		$("#ifagree").attr("checked",false);
	}
	
	
/*	$(".modifyER").click(function(){											//编辑归档

		$("textarea.eventReportString").attr("readonly","");
		$("textarea.eventReportString").css("background-color","#ffffff");
	});*/
	

	
	/****************************************/
	function result_alert(result) {
		$("div[name='alert']").text(result).dialog('open');
	}
	$("div[name='alert']").dialog( {
		autoOpen : false,
		height : 200,
		width : 350,
		modal : true,
		buttons : {
			'确定' : function() {
				window.location.href="/ag/wzp/event_findById.action?event.EId="+eventId;		//回到事件详情中
		  			}
		},
		close : function() {
			
		}
	});
	$("#eventReport").dialog( {
		autoOpen : false,
		height : 100,
		width : 200,
		modal : true,
		buttons : {
			'确定' : function() {
		  			$("#eventReport").dialog('close');	
			}
		},
		close : function() {
			allFields.val('').removeClass('ui-state-error');
		}
	});


	function submitER(){											//提交办结
		var eventReportDOM = $("#xhe0_iframe").contents();
		var eventReport=$(eventReportDOM).find(".editMode");
		var eventReportString = $(eventReport).html();
		var bValid = true;
		/* $("button[type='button']").attr("disabled","disabled"); */
		bValid = bValid && checkLength(eventReportString, "内容", 1, 4000);
	
		if (!bValid) {
			$("#eventReport").dialog('open');
		}else{
		req = {
			'tbEventIdString':	eventId,
			'eventReportString': eventReportString
		}
		var url= "/ag/dgm/submitEventReportString_eventReport.action";
		$.post(url,req,function(data){
			result_alert(data.result);
		});
		}
	}

	function checkLength(o, n, min, max) {						//确定长度，汉字算两个，英文算一个 o为内容,
		var dtDealingType1 = $.trim(o);
		var cArr1 = dtDealingType1.match(/[^x00-xff]/ig);
		var the_length = dtDealingType1.length
				+ (cArr1 == null ? 0 : cArr1.length);
		if (the_length > max || the_length < min) {
			return false;
		} else {
			return true;
		}
	}


});







